package com.itheima.web.controller;

import cn.hutool.captcha.LineCaptcha;
import com.itheima.domain.db.Admin;
import com.itheima.web.manager.AdminManager;
import org.apache.tomcat.util.http.parser.Authorization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

/**
 * @Author 请不要叫我高司令
 * @Date 2022/3/24 16:49
 * @Version 1.0
 */

@RestController
public class AdminController {


    @Autowired
    private AdminManager adminManager;

    //生成验证码
    @GetMapping("/system/users/verification")
    public void verification(String uuid, HttpServletResponse response) throws IOException {

        LineCaptcha lineCaptcha = adminManager.verification(uuid);

        lineCaptcha.write(response.getOutputStream());

    }

    //用户登录
    @PostMapping("/system/users/login")
    public ResponseEntity login(@RequestBody HashMap<String, String> map) {

        String username = map.get("username");
        String password = map.get("password");
        String verificationCode = map.get("verificationCode");
        String uuid = map.get("uuid");

        return adminManager.login(username, password, verificationCode, uuid);


    }


    //校验token-获取用户基本信息
    @PostMapping("/system/users/profile")
    public ResponseEntity profile(@RequestHeader("Authorization") String token) {

        Admin admin = adminManager.findAdminByToken(token);

        return ResponseEntity.ok(admin);

    }

    //用户登出
    @PostMapping("/system/users/logout")
    public void logout(@RequestHeader("Authorization") String token) {

        adminManager.logout(token);

    }





}
